<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ExamineNoteComment;
use App\Admin\Repositories\HealthNoteComment;
use App\Constants\HealthNoteConstant;
use App\Models\HealthNote;
use App\Models\Member;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HealthNoteCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HealthNoteComment(['note', 'member']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->column('note_id');
            $grid->column('note.title', '笔记标题')->limit(10);
            $grid->column('pusher_id');
            $grid->column('member.nickname', '发布人昵称');
            $grid->column('content')->limit(15);
            $grid->column('examine_status')->using(HealthNoteConstant::EXAMINE_STATUS_MAP)->dot(['warning', 'danger', 'success']);
            $grid->column('fav_count');
            $grid->column('is_show')->switch();
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($actions->row->examine_status == HealthNoteConstant::WAIT_EXAMINE) {
                    $actions->append(new ExamineNoteComment(\App\Models\HealthNoteComments::class));
                }
            });

            $grid->disableEditButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->equal('note_id')->width(3);
                $filter->like('note.title', '笔记标题')->width(3);
                $filter->equal('pusher_id')->width(3);
                $filter->equal('examine_status')->select(HealthNoteConstant::EXAMINE_STATUS_MAP)->width(3);
                $filter->between('created_at')->datetime()->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HealthNoteComment(), function (Show $show) {
            $show->field('id');
            $show->field('note_id');
            $show->field('pusher_id');
            $show->field('examine_status');
            $show->field('examine_time');
            $show->field('fav_count');
            $show->field('is_show');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HealthNoteComment(), function (Form $form) {
            $form->display('id');
            $form->select('note_id')->options(HealthNote::pluck('title', 'id')->all());
            $form->select('pusher_id')->options(Member::pluck('nickname', 'id')->all());
            $form->text('content');
            $form->select('examine_status')->options(HealthNoteConstant::EXAMINE_STATUS_MAP)->default(HealthNoteConstant::WAIT_EXAMINE);
            $form->hidden('examine_time')->saving(function ($v) {
                return (int)$v;
            });
            $form->number('fav_count');
            $form->radio('is_show')->options(['隐藏', '显示'])->default(1);

            $form->saving(function (Form $form) {
                if ($form->isCreating() && $form->examine_status != HealthNoteConstant::WAIT_EXAMINE) {
                    $form->examine_time = time();
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
